package com.test.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

abstract class Shape implements Serializable {
	public static final int RED = 1, BLUE = 2, GREEN = 3;
	private int xPos, yPos, dimension;
	private static Random rand = new Random(47);
	private static int counter = 0;

	public abstract void setColor(int newColor);

	public abstract int getColor();

	public void serializeStaticsState(ObjectOutputStream out)
			throws IOException {
		System.out.println("called");
		out.defaultWriteObject();
	}

	public void deserializeStaticState(ObjectInputStream in)
			throws IOException, ClassNotFoundException {
		in.defaultReadObject();
	}

	public Shape(int xVal, int yVal, int dim) {
		xPos = xVal;
		yPos = yVal;
		dimension = dim;
	}

	public String toString() {
		return getClass() + " color[" + getColor() + "] xPox[ " + xPos
				+ "] yPos[ " + yPos + "] dim[ " + dimension + "]\n";
	}

	public static Shape randomFactory() {
		int xVal = rand.nextInt(100);
		int yVal = rand.nextInt(100);
		int dim = rand.nextInt(100);
		switch (counter++ % 3) {

		default:
		case 0:
			return new Circle(xVal, yVal, dim);
		case 1:
			return new Square(xVal, yVal, dim);
		case 2:
			return new Line(xVal, yVal, dim);
		}
	}
}

class Circle extends Shape {
	private static int color = RED;

	public Circle(int xVal, int yVal, int dim) {
		super(xVal, yVal, dim);
	}

	@Override
	public void setColor(int newColor) {
		color = newColor;

	}

	@Override
	public int getColor() {

		return color;
	}

	@Override
	public void serializeStaticsState(ObjectOutputStream out)
			throws IOException {
		super.serializeStaticsState(out);
		out.writeInt(color);

	}

	@Override
	public void deserializeStaticState(ObjectInputStream in)
			throws IOException, ClassNotFoundException {
		super.deserializeStaticState(in);
		color = in.readInt();

	}

}

class Square extends Shape {

	private static int color;

	public Square(int xVal, int yVal, int dim) {
		super(xVal, yVal, dim);
		color = RED;
	}

	@Override
	public void setColor(int newColor) {
		color = newColor;

	}

	@Override
	public int getColor() {
		return color;
	}

	@Override
	public void serializeStaticsState(ObjectOutputStream out)
			throws IOException {
		super.serializeStaticsState(out);
		out.writeInt(color);

	}

	@Override
	public void deserializeStaticState(ObjectInputStream in)
			throws IOException, ClassNotFoundException {
		super.deserializeStaticState(in);
		color = in.readInt();

	}

}

class Line extends Shape {
	private static int color = RED;

	public Line(int xVal, int yVal, int dim) {
		super(xVal, yVal, dim);
	}

	@Override
	public void setColor(int newColor) {
		color = newColor;

	}

	@Override
	public int getColor() {
		return color;
	}

	@Override
	public void serializeStaticsState(ObjectOutputStream out)
			throws IOException {
		super.serializeStaticsState(out);
		out.writeInt(color);

	}

	@Override
	public void deserializeStaticState(ObjectInputStream in)
			throws IOException, ClassNotFoundException {
		super.deserializeStaticState(in);
		color = in.readInt();

	}

}

public class StoreCADState {

	public static void main(String[] args) throws IOException {
		// List<Class<? extends Shape>> shapeType = new ArrayList<Class<?
		// extends Shape>>();
		// add references to the class objects:
		// shapeType.add(Circle.class);
		// shapeType.add(Square.class);
		// shapeType.add(Line.class);
		List<Shape> shapes = new ArrayList<Shape>();
		// Make some shapes:
		for (int i = 0; i < 10; i++) {
			shapes.add(Shape.randomFactory());
			//
		}
		for (int i = 0; i < 10; i++) {
			((Shape) shapes.get(i)).setColor(Shape.GREEN);

		}
		// save the state vector:
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				"CADState.out"));
		// out.writeObject(shapeType);
		// Line.serializeStaticsState(out);
		
		out.writeObject(shapes);
		// display the shapes:
		System.out.println(shapes);
	}
}
